package vn.edu.saigontech.ncms.model.bookstore;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.OptimisticLockType;

import vn.edu.saigontech.ncms.model.BaseEntity;
import vn.edu.saigontech.ncms.model.system.Money;

@Entity
@org.hibernate.annotations.Entity(dynamicInsert = true, dynamicUpdate = true, optimisticLock = OptimisticLockType.ALL)
@Table(name = "ncms_bookstore_item")
@SuppressWarnings("serial")
public class Item extends BaseEntity {
	
	@ManyToOne
	@JoinColumn(name = "item_title_id")
	private ItemTitle itemTitle;

	@ManyToOne
	@JoinColumn(name = "item_type_id")
	private ItemType itemType;

	@Column(name = "item_id", unique = true, nullable = false)
	private String itemId;

	@Embedded
	@AttributeOverrides( {
			@AttributeOverride(name = "amount", column = @Column(name = "price")),
			@AttributeOverride(name = "currency", column = @Column(name = "price_currency")), })
	private Money price;

	@Column(name = "quantity")
	private int quantity;

	@Column(name = "active")
	private boolean active;
	
	@Column(name = "comment")
	private String comment;
	
	public String getDisplayName() {
		return this.itemTitle.getTitle() + " (" + this.itemType.getName() + ")";
	}
	
	public void increaseQuantity(int value) {
		this.quantity = this.quantity + value;
	}
	
	public void decreseQuantity(int value) {
		this.quantity = this.quantity - value;
	}
	
	public boolean isBook() {
		String itemType = this.getItemType().getName();
		return ItemType.SB.equals(itemType) || ItemType.WB.equals(itemType);
	}
	
	public boolean isCD() {
		String itemType = this.getItemType().getName();
		return ItemType.CD.equals(itemType);
	}

	public ItemTitle getItemTitle() {
		return itemTitle;
	}

	public void setItemTitle(ItemTitle itemTitle) {
		this.itemTitle = itemTitle;
	}

	public ItemType getItemType() {
		return itemType;
	}

	public void setItemType(ItemType itemType) {
		this.itemType = itemType;
	}

	public Money getPrice() {
		return price;
	}

	public void setPrice(Money price) {
		this.price = price;
	}

	public int getQuantity() {
		return quantity;
	}

	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}

	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}

	public String getItemId() {
		return itemId;
	}

	public void setItemId(String itemId) {
		this.itemId = itemId;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}
}